/*
* Copyright 2016 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-wechat-open-api
*
* @File name : DealerWechatMessageController.java
*
* @Author : LiuJun
*
* @Date : 2016年12月6日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2016年12月6日    LiuJun    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmcc.gateway.wechat.controller.message;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmcc.gateway.wechat.common.ApiVersionConsts;
import com.yonyou.gmcc.gateway.wechat.service.interfaces.DealerWechatMessageApiServiceInterface;

/**
*
* @author LiuJun
* 接收经销商微信服务号消息controller
* @date 2016年12月6日
*/
@RestController
@RequestMapping(value = ApiVersionConsts.BASE_PATH +"/dealer/msg")
public class DealerWechatMessageController {
    
    private static Logger logger = Logger.getLogger(DealerWechatMessageController.class);

    
    @Autowired
    private DealerWechatMessageApiServiceInterface dealerWechatMessageApiServiceInterface;
    
    /**
    *
    * @author LiuJun
    * 接收经销商公众号消息入口
    * @date 2016年12月6日
    * @param dealerAppid
    * @param nonce 随机数
    * @param timestamp 时间戳
    * @param signature 微信加密签名
    * @param echostr 随机字符串
    * @param xml
    * @return
    * @throws Exception
    */
    @RequestMapping(value = "/{dealerAppid}", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String receiveDealerWeChatMsg(@PathVariable("dealerAppid") String dealerAppid, 
                                         @RequestParam("nonce") String nonce,
                                         @RequestParam("timestamp") String timestamp,
                                         @RequestParam("msg_signature") String signature,
                                         @RequestBody String xml,
                                         HttpServletRequest request, HttpServletResponse response) {
        logger.info("@receiveDealerWeChatMsg-getQueryString="+request.getQueryString());
        logger.info("@receiveDealerWeChatMsg-getRequestURI="+request.getRequestURI());
        logger.info("@receiveDealerWeChatMsg-dealerAppid="+dealerAppid);
        logger.info("@receiveDealerWeChatMsg-nonce="+nonce);
        logger.info("@receiveDealerWeChatMsg-timestamp="+timestamp);
        logger.info("@receiveDealerWeChatMsg-signature="+signature);
        logger.info("@receiveDealerWeChatMsg-xml="+xml);
        String resultMsg =  dealerWechatMessageApiServiceInterface.receiveDealerWeChatMsg(dealerAppid, nonce, timestamp, signature, xml);
        
        logger.info("@receiveDealerWeChatMsg-resultMsg="+resultMsg==null?"":resultMsg);

        return resultMsg;
    }


}
